package com.andyliu.configer;

import com.alibaba.druid.support.http.StatViewFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * ClassName: WebConfiger <br/>
 * Description: <br/>
 * date: 2021/1/18 5:07 下午<br/>
 *
 * @author andyliu<br />
 * @since JDK 1.8
 */

public class WebConfiger  extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected java.lang.Class<?>[] getRootConfigClasses() {
        return new java.lang.Class[]{RootConfiger.class};
    }

    @Override
    protected java.lang.Class<?>[] getServletConfigClasses() {
        return new java.lang.Class[]{MvcConfiger.class};
    }

    @Override
    protected java.lang.String[] getServletMappings() {
        return new java.lang.String[]{"/"};
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        FilterRegistration.Dynamic encodingFilter=servletContext.addFilter("characterEncodingFilter",new CharacterEncodingFilter());
        encodingFilter.setInitParameter("encoding","UTF-8");
        encodingFilter.setInitParameter("forceEncoding", "true");
        encodingFilter.addMappingForUrlPatterns(null, true, "/*");
       // filter过滤器
        FilterRegistration.Dynamic shiroFilter = servletContext.addFilter("shiroFilter", new DelegatingFilterProxy());
        shiroFilter.setInitParameter("targetFilterLifecycle","true");
        shiroFilter.addMappingForUrlPatterns(null,true,"/*");
        // druid的servlet
        ServletRegistration.Dynamic statViewServlet = servletContext.addServlet("statViewServlet", new StatViewServlet());
        statViewServlet.setInitParameter("loginUsername","root");
        statViewServlet.setInitParameter("loginPassword","Lh_198571");
        statViewServlet.addMapping("/druid/");
        // druid过滤器
        FilterRegistration.Dynamic druidFilter = servletContext.addFilter("druidFilter", new StatViewFilter());
        druidFilter.setInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        druidFilter.addMappingForUrlPatterns(null,true,"/*");

    }


}
